<template>
	<view class="page-fade-in" :style="{opacity: pageOpc}">
		<template v-if="pageOpc">
			<view class="content">
				<view class="head-box">
					<view class="date-box flexWrapNo" @tap="showDate()">
						<image src="/static/index/calendar.png" class="calendar-icon"></image>
						{{starTime}}～{{endTime}}
					</view>
					<view class="date-boxs" v-if="dateFlag">
						<view class="date-title">时间筛选</view>
						<view class="date-cont flexWrap">
							<view class="screen-item flexWrap">
								<picker mode="date" :data-index='0' :value="starTime" @change="bindDateChange" :end="date">
									<view class="select-input" v-if="starTime">{{starTime}}</view>
									<view class="select-input" v-else>请选择开始时间</view>
								</picker>
								<text class="triangle"></text>
							</view>
							<view class="screen-item flexWrap">
								<picker mode="date" :data-index='1' :value="date" @change="bindDateChange" :start="starTime"
									:end="date">
									<view class="select-input" v-if="endTime">{{endTime}}</view>
									<view class="select-input" v-else>请选择结束时间</view>
								</picker>
								<text class="triangle"></text>
							</view>
						</view>
						<view class="determine-btn flexWrapNo">
							<!-- <view @tap="reSet()">重置</view> -->
							<view class="determine" @tap="submit()">确认</view>
						</view>
					</view>
				</view>
				<view class="order-box">
					<view class="order-item flexWrap" v-for="(item,index) in orderList" :key='index' @tap="goOrderDetails(item.id)">
						<view class="order-info">
							<view class="order-info-text">时间：{{item.createtime}}</view>
							<view class="order-info-text" v-if="item.order_id">订单：{{item.order_id}}</view>
							<view class="order-info-text">操作日志：{{item.remarks}}</view>
						</view>
					</view>
					<uni-load-more v-if="showLoadStatus" :status="status" :size="14" :content-text="contentText" />
					<view class="empty-box" v-if="showEmpty">
						<image src="https://static.mo-yu.vip/wechat/images/orderEmpty.png"></image>
					</view>
				</view>
				<view class="mask-wrap" v-if="dateFlag" @tap="closeDate()"></view>
			</view>
		</template>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue'
	export default {
		components: {
			uniLoadMore
		},
		data() {
			const currentDate = this.getDate({
				format: true
			})
			const starDate = this.getDate('start')
			return {
				pageOpc:0,
				currmentIndex:0,
				//下拉刷新
				page: 1,
				size:15,
				showEmpty: true,
				showLoadStatus: false,
				more: true,
				contentText: {
					contentdown: '上拉加载更多~',
					contentrefresh: '加载中...',
					contentnomore: '已经到底了~~'
				},
				status: 'loading',
				orderList:[],
				count:{},
				starTime: starDate,
				endTime: currentDate,
				date: currentDate,
				dateFlag:false,
				equipment_id:""
			}
		},
		onLoad(ops) {
			this.equipment_id=ops.equipment_id
			this.freshList();
		},
		onReady(){
			setTimeout(() => {
				this.pageOpc=1
			}, 100)
		},
		// 触底刷新
		onReachBottom() {
			if (this.more) {
				this.getData()
			}
		},
		// 下拉刷新
		onPullDownRefresh: function() {
			this.freshList();
			this.$tools.loadIng()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 500)
		},
		methods: {
			// 获取时间
			getDate(type) {
				const date = new Date();
				if (type === 'start') {
					date.setDate(date.getDate());
				}
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			// 选择时间
			bindDateChange(e) {
				let index = e.currentTarget.dataset.index
				if (index == 0) {
					this.starTime = e.target.value
				} else {
					this.endTime = e.target.value
				}
			},
			// 重置
			reSet() {
				this.starTime = this.getDate('start')
				this.endTime = this.getDate({
					format: true
				})
			},
			// 提交
			submit() {
				this.freshList();
				this.closeDate();
			},
			showDate(){
				this.dateFlag=!this.dateFlag
			},
			closeDate(){
				this.dateFlag=false
			},
			// 刷新列表
			freshList() {
				this.page = 1
				this.more = true
				this.status = 'loading'
				this.orderList = []
				this.getData();
			},
			// 获取订单数据
			getData(){
				let self=this
				this.$request.post(this.$api.equipmentLog, {
					page:this.page++,
					num:this.size,
					store_id:uni.getStorageSync('store_id'),
					start_time:this.starTime,
					end_time:this.endTime,
					equipment_id:this.equipment_id
				}, {
					token:true
				}).then(res => {
					if (res.code == 1) {
						let {
							data = {}
						} = res;
						let newArray =data.list
						if (newArray.length == 0 && self.page == 2) {
							self.showEmpty = true
							self.showLoadStatus = false
							self.more = false // 不能再次请求了
							return false;
						} else {
							self.showLoadStatus = true
							self.more = true
							self.showEmpty = false
							self.status = 'loading'
						}
						if (newArray.length < this.size) { // 说明没有更多数据
							self.status = 'noMore'
							self.more = false
							self.showEmpty = false
						}
						self.orderList=self.orderList.concat(newArray);
					}
				}).finally(() => {
					setTimeout(()=>{
						this.$tools.hideLoading()
					}, 1000)
				})
			},
		}
	}
</script>

<style lang="scss">
	.head-box{
		padding: 20upx 34upx;
	}
	.mask-wrap {
		position: fixed;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
		margin: auto;
		z-index: 99;
		background: rgba(0, 0, 0, .4);
	}
	.date-box{
		font-size: 32upx;
		font-family: PingFang SC;
		font-weight: 500;
		line-height: 60upx;
		color: #333;
		.calendar-icon{
			width: 48upx;
			height: 48upx;
			margin-right: 24upx;
		}
	}
	.date-boxs{
		padding: 30rpx;
		z-index: 999;
		border-bottom: 1rpx solid rgba(71, 73, 84, .5);
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		background-color: #040336;
		.date-title {
			font-size: 30rpx;
			font-family: PingFang;
			font-weight: 500;
			color: #FFFFFF;
			margin-bottom: 26rpx;
		}
	
		.date-cont {
			.screen-item {
				width: 250rpx;
				height: 66rpx;
				line-height: 66rpx;
				text-align: center;
				border: 1rpx solid #26BCFD;
				color: #323232;
				font-size: 28rpx;
				border-radius: 6rpx;
				padding: 0 30rpx;
	
				.select-input {
					font-size: 26rpx;
					font-family: PingFang;
					font-weight: 400;
					color: #26BCFD;
				}
	
				picker {
					width: 100%;
					text-align: left;
				}
	
				.triangle {
					border-top-color: #333;
				}
			}
		}
		.determine-btn {
			margin-top: 40rpx;
			justify-content: center;
			view {
				width: 230rpx;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				font-size: 26rpx;
				font-family: PingFang;
				font-weight: 500;
				color: #26BCFD;
				border: 1rpx solid #26BCFD;
				border-radius: 20rpx;
				margin-right: 30upx;
			}
		
			.determine {
				background:#26BCFD;
				color: #FFFFFF;
				border: none;
			}
		}
	}
	.order-box{
		padding: 0 36upx;
		.order-item{
			align-items: flex-start;
			padding-top: 26upx;
			padding-bottom: 20upx;
			border-bottom: 1upx solid #36355E;
			&:nth-of-type(1){
				border-top: 1upx solid #36355E;
			}
			.order-info{
				.order-info-text{
					font-size: 24upx;
					font-family: PingFang SC;
					font-weight: 400;
					line-height: 40upx;
					color: #333;
				}
			}
			.order-status{
				font-size: 24upx;
				font-family: PingFang SC;
				font-weight: 400;
				line-height: 36upx;
				color: #FFFFFF;
				flex:none;
			}
		}
		.empty-box{
			width: 476upx;
			height: 280upx;
			margin:250upx auto;
		}
	}
</style>
